import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/util/DbUtil.dart';
import 'package:im_app/im/util/ImSingleton.dart';

import '../../pojo/Enums.dart';
import '../../pojo/ImPojo.dart';
import '../../service/ImModuleService.dart';

final class WorkbenchCubit extends Bloc<WorkbenchEvent, WorkbenchState> {
  WorkbenchCubit() : super(const WorkbenchState.loading()) {
    on<WorkbenchRouterEvent>((event, emit){
      _router(this.state.modules, this.state.historyModule, event.moduleItem);
    });
  }

  // final _moduleService = ImModuleService();

  Future<List<ImModule>> loadData(ImModuleItem? moduleItem) async {
    String userId = ImSingleton.getInstance().session.userId!;
    final moduleService = ImModuleService();
    List<ImModule> modules = await moduleService.getModules();
    ImModule? historyModule = await moduleService.getHistoryModule(userId);
    print("workbench load Data!!!!!!!!!!!!!");
    emit(WorkbenchState.success(modules, historyModule, moduleItem));
    return modules;
  }

  void _router(List<ImModule> modules, ImModule? historyModule, ImModuleItem moduleItem) async {
    String userId = ImSingleton.getInstance().session.userId!;
    final moduleService = ImModuleService();
    if (moduleItem.name.contains("_")) {
      ImModule newHistoryModule = ImModule(name: userId + '_history', status: ImModuleStatus.enable, no: 1000);
      await moduleService.update(newHistoryModule);
      emit(WorkbenchState.success(modules, historyModule, moduleItem));
    } else {
      ImModule newHistoryModule = ImModule(name: userId + '_history', status: ImModuleStatus.enable, no: 1000);
      ImModuleItem newImModuleItem = new ImModuleItem(name: "${moduleItem.moduleId}_${moduleItem.name}",
          no: 1000,
          icon: '',
          status: ImModuleItemStatus.enable,
          type: moduleItem.type);
      newHistoryModule.moduleItemList = [newImModuleItem];
      await moduleService.saveOrUpdate(newHistoryModule);
      await loadData(moduleItem);
    }
  }
}

final class WorkbenchState extends Equatable {
  final LoadingState state;
  final List<ImModule> modules;
  final ImModule? historyModule;
  final ImModuleItem? selectModuleItem;

  const WorkbenchState._({
    this.state = LoadingState.loading,
    this.modules = const <ImModule>[],
    this.historyModule = null,
    this.selectModuleItem = null,
  });

  const WorkbenchState.loading() : this._();

  const WorkbenchState.success(List<ImModule> modules, ImModule? historyModule, ImModuleItem? moduleItem)
      : this._(state: LoadingState.success, modules: modules, historyModule: historyModule, selectModuleItem:
  moduleItem);

  const WorkbenchState.failure() : this._(state: LoadingState.failure);

  @override
  List<Object?> get props => [state, modules, historyModule, selectModuleItem];
}

abstract class WorkbenchEvent {}
class WorkbenchRouterEvent extends WorkbenchEvent {
  ImModuleItem moduleItem;
  WorkbenchRouterEvent(this.moduleItem);
}